package com.qf.zyh.web.controller;

import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.entity.Menu;
import com.qf.zyh.entity.Role;
import com.qf.zyh.services.MenuServices;
import com.qf.zyh.services.RoleServices;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServices roleServices;
    @Autowired
    private MenuServices menuServices;

    @RequestMapping("/table")
    public ResultType table(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String name){
        ResultType rt=roleServices.getRoleByPage(page,limit,name);
        return rt;
    }
    @RequestMapping("/role_tree")
    public List<Role> getRole(){
        return roleServices.getRole();
    }



    @RequestMapping("/updateStu")
    public ResponseResult updateStu(Integer id,Integer status){
        ResponseResult rt = roleServices.updaStatusByid(id,status);
    return rt;
    }
    @RequestMapping("/update")
    public ResponseResult updateRole(Role l){
        ResponseResult rt = roleServices.updaRole(l);
    return rt;
    }
    @RequestMapping("/add")
    public ResponseResult addRole(Role l){
        ResponseResult rt = roleServices.addRole(l);
    return rt;
    }


    @RequestMapping("/role_menu")
    public List<Integer> getRoleMenu(Integer roleId){
        List<Integer> menuByUid = roleServices.getRoleMenu(roleId);
        return menuByUid;
    }
    @RequestMapping("/menu_tree")
    public List<Menu> getAllMenu(){
        return menuServices.getMenu();
    }
    @RequestMapping("/assign_menu")
    public ResponseResult getassignMenu(Integer roleId,Integer[] menuIds){
        try {
            return roleServices.updaMenu(roleId,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }
}
